package tasks;

import static java.lang.Math.nextAfter;
import Abstracts.AbstractControlSystem;
import Abstracts.AbstractFunction;

public class BoilerControlSystem implements AbstractControlSystem,Cloneable {

	AbstractFunction[] functions;
	int dimensions = 5;
	
	public BoilerControlSystem clone() throws CloneNotSupportedException{
		BoilerControlSystem obj=(BoilerControlSystem)super.clone();
		obj.functions = new AbstractFunction[functions.length];
		for (int i=0;i!=functions.length;++i){
			obj.functions[i] = functions[i].clone();
		}
		
		obj.dimensions = dimensions;
        return obj;
	}
	
	
/*
	public void clone(AbstractControlSystem cs){
		AbstractFunction[] newFunctions = new AbstractFunction[this.functions.length];
		
		for (int i=0;i!=newFunctions.length;++i){
			this.functions[i].clone(newFunctions[i]);
		}
		cs = new BoilerControlSystem();
		cs.setControlSystem(newFunctions);
	}
	*/
	public double[] getMove(double[] params) {

		if (functions.length != dimensions) {
			throw new UnsupportedOperationException(
					"For boiler shoud be difined" + dimensions + "functions");
		}

		double max = Double.NEGATIVE_INFINITY;
		double index = -1;
		for (int i = 0; i != dimensions; ++i) {
			double current = functions[i].compute(params);
			if (current > max) {
				max = current;
				index = i;
			}
		}

		if (index == -1) {
			throw new UnsupportedOperationException(
					"Problems with funtions calculation");
		} else if (index == 0) {
			return new double[] { 100 };
		} else if (index == 1) {
			return new double[] { 75 };
		} else if (index == 2) {
			return new double[] { 50 };
		} else if (index == 3) {
			return new double[] { 25 };
		} else {
			return new double[] { 0 };
		}
	}

	public BoilerControlSystem(AbstractFunction[] funcions){
		this.functions = funcions;
	}
	
	public BoilerControlSystem(){
		generateControlSystem();
	}
	
	public void setControlSystem(AbstractFunction[] funcions) {
		this.functions = funcions;
	}

	public AbstractFunction[] getFunctions() {
		return functions;
	}

	public void generateControlSystem() {
		if (functions == null) {
			functions = new AbstractFunction[dimensions];
		}
	}

	public int getDimensions() {
		return dimensions;
	}
	
}
